﻿/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
var _id_step = 1;

$(document).ready( function (e) {
    $('#cancel').click( function () { 
	$.gotoStep(1);
    });

	/*
	
    var v = jQuery("#form").validate({
	      submitHandler: function(form) {
                    jQuery(form).ajaxSubmit({
                            beforeSubmit: function () {
                                 $('#conteneur').html('Veuillez patienter...');
                            },
                            success: function (msg) {
                                    $('#lipsum').html(msg);
                                    setTimeout('window.document.location.href="./";', 3000);
                            }
                    });
            }
    });
	
	*/

    $('.required').parent().prev().each( function (e) { 
		$(this).append('&nbsp;&nbsp;&nbsp;*');
    });

    // on démarre
    $("select[multiple]").asmSelect({
            removeLabel: '<img src="../images/removeList.png" align="absmiddle" border="0" />',
            highlightAddedLabel: 'Ajouter : ',
            highlightRemovedLabel: 'Supprimer : ',
            addItemTarget: 'bottom',
            animate: true,
            highlight: true,
            sortable: false
    });

    // on masque toutes les étapes
    $('.steps').hide();

    // on créé le titre de l'étape en cours
    $('#step' + _id_step).show('fold', function (e) {
        $.navigatorStepper();
    });

    // on créé le menu
    $('#titre-step' + _id_step).hide();

    _titre_step = $('#titre-step' + _id_step).html();

    _stepper = $.initStepper();

    $('#colonneGauche').html(_stepper);

    // liens stepper
    $('.steppers').click( function (e) {
        if ($(this).hasClass('selstep')) {
            return false;
        }
        else {
            $.gotoStep($(this).attr('id'));
        }
    });

    // remplace les Opcrir
    $('.Opcrir').each(function (e) {
        $parent = $(this).parent();
        $label = $parent.find('label').eq(0);
        $label.addClass('isCheckbox');
        $label.click( function (e) {
            $input = $('input#' + $(this).attr('for'));
            if ($input.attr('checked')) {
                $(this).removeClass('isChecked');
            }
            else {
                $(this).addClass('isChecked');
            }
        });
    });
    
    // remplace les Opcrir
    $('.OpcrirAutres').each(function (e) {
        $parent = $(this).parent();
        $label = $(this).prev();
        $label.addClass('isCheckbox');
        $label.click( function (e) {
            $input = $('input#' + $(this).attr('for'));
            if ($input.attr('checked')) {
                $(this).removeClass('isChecked');
            }
            else {
                $(this).addClass('isChecked');
            }
        });
    });

   // remplace les Opcrir
    $('.OpRadio').each(function (e) {
        $(this).click( function (e) {
             $(this).parent().find('.radioBox').removeClass('isSelected');
	     $(this).prev().addClass('isSelected');
        });
    });

});

(function($){
	$.gotoStep = function(_id) {
	     stop = false;
            for(_i=1;_i<_id; _i++) {
                if (stop==false) {
                    //_id = _i + 1;
                    // ici on regarde si la saisie de tous les obligatoires ok ?
                    $('#step' + _i).find('.required').each(function(e) {
                        if ($(this).val() == '' || $(this).hasClass('error')) {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> *</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type numéro de SIRET
                    $('#step' + _i).find('.siret_number').each(function(e) {
						var pattern = new RegExp('^[0-9]{15}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer un nombre composé de 15 chiffres</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type number
                    $('#step' + _i).find('.digits').each(function(e) {
						var pattern = new RegExp('^[0-9]{1,}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer un nombre valide</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type post_code
                    $('#step' + _i).find('.post_code').each(function(e) {
						var pattern = new RegExp('^[0-9]{5}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer un code postal valide</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type phone_number
                    $('#step' + _i).find('.phone_number').each(function(e) {
						var pattern = new RegExp('^0[0-9]([ .-]?[0-9]{2}){4}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer un numéro de téléphone valide</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type fax_number
                    $('#step' + _i).find('.fax_number').each(function(e) {
						var pattern = new RegExp('^0[0-9]([ .-]?[0-9]{2}){4}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer un numéro de fax valide</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });
					
                    // Vérification des champs de type email
                    $('#step' + _i).find('.email').each(function(e) {
					var pattern = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$');
                        if (!pattern.test($(this).val()) && $(this).val() != '') {
                            $('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $(this).attr('name')).remove();
				$(this).css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $(this).attr('name') + '"> Veuillez entrer une adresse email valide</span>').insertAfter($(this));
                            stop = true;
                        }
                        else {
							$(this).css('border', '1px solid #ccc');
                            $(this).parent().find('#err-' + $(this).attr('name')).eq(0).remove();
                            stop = (stop == true) ? true : false;
                        }

                    });

                }


		  // gestion du nouveau mot de passe
		  if (_i == 2 && ( $('#password_confirm').val() != $('#password').val() )) {
		      		$('#o-stepper #' + _i).addClass('red');

                            if (_i != _id_step) {
                                $.gotoStep(_i);
                                _id_step = _i;
                            }
                            $('span#err-' + $('#password_confirm').attr('name')).remove();
				$('#password_confirm').css('border', '1px solid #990000');
				$('span#err-' + $('#password').attr('name')).remove();
				$('#password').css('border', '1px solid #990000');

                            $('<span class="error-form" id="err-' + $('#password_confirm').attr('name') + '"> *</span>').insertAfter($('#password_confirm'));
				$('<span class="error-form" id="err-' + $('#password').attr('name') + '"> *</span>').insertAfter($('#password'));
                            stop = true;
		  }

                if (stop == false) {
                    $('#o-stepper #' + _i).removeClass('red');
                }

            }
	     if (stop) {return false;}
            else {

                $('.selstep').removeClass('selstep');
                $('.steppers').each( function (e) {
		      if ($(this).attr('id') == _id) {
                        $(this).fadeOut('200', function (e) {
                            $(this).addClass('selstep');
                            $(this).fadeIn('200', function (e) {
                                // on masque toutes les étapes
                                $('.steps').hide('fold');
                                _id_step = _id;

                                // on créé le titre de l'étape en cours
                                $('#step' + _id).show('fold', function (e) {
                                    $.navigatorStepper();
                                });
				    // on créé le menu
                                $('#titre-step' + _id).hide();
				    $('html, body').animate({scrollTop:0}, '1000');

                            });
                        });
                    }
                });
            }
        };
})(jQuery);

(function($){
	$.initStepper = function() {

            _stepper = '<div id="o-stepper">';
            _cstep = 0;

            $('.steps').each( function (e) {
                if ($(this).attr('id') == 'step' + _id_step) {
                    _selected = _cstep;
                }
                _cstep++;

            });
            
            // on a tous les stepper
            // on génère
            i = 0;
            for( i=0;i<_cstep;i++) {

                _c = '';
                // création du stepper
                if (_selected == i) { 
                    _c = 'selstep';
                }
               
                _stepper += '<div class="steppers ' + _c + '" id="'+ (i+1) +'">' + (i+1)+ ': '+ $('#titre-step' + (i+1)).html() +'</div>';

            }
           
            return _stepper + '</div>';
        };
})(jQuery);


(function($){
	$.password_forgot = function() {
	 	$.nyroModalManual({url:'./password.html', 
				autoSizable: true,
				minWidth: 200,
				minHeight: 100,
				zIndexStart: 99999
		});
	};

})(jQuery);
	
(function($){
	$.navigatorStepper = function() {
            // on regarde l'id step
            
            $('#nav-steppter').remove();
            $count =  0;
            $('.steps').each( function (e) {
                $count++;
            });
            
	     $next = parseInt(_id_step)+1;
            $prev = parseInt(_id_step)-1;



            // création du mode navigatio
            if (_id_step == 1) { // démarrage
                $navigator = $('<div id="nav-steppter"><img class="navigations" onclick="$.gotoStep('+ $next +')" src="../images/nav-next.png" /></div>');
            }
            else if (_id_step == $count) {
                $navigator = $('<div id="nav-steppter"><img  onclick="$.gotoStep('+ $prev +')" class="navigations" src="../images/nav-prev.png" /></div>');
            }
            else {
                $navigator = $('<div id="nav-steppter"><img class="navigations"  onclick="$.gotoStep('+ $prev +')" src="../images/nav-prev.png" /> &nbsp; <img class="navigations"  onclick="$.gotoStep('+ $next +')" src="../images/nav-next.png" /></div>');
            }
            $('#step' + _id_step).append($navigator);
            $navigator.fadeIn(500);

        };
})(jQuery);
